"""
Anthropic SDK 简单使用示例
"""

from anthropic import Anthropic

# 配置
api_key = "cr_a80f97eb77a496c000e17f06f472c0d57e9d33f32ec565fb853ee088559303e2"
base_url = "http://154.12.94.140:3000/api"  # 注意：不要加 /v1，SDK会自动添加

# 创建客户端
client = Anthropic(
    api_key=api_key,
    base_url=base_url,
)

# 1. 简单对话
print("1. 简单对话示例")
print("-" * 40)

response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=100,
    messages=[
        {"role": "user", "content": "Hello! Please introduce yourself in one sentence."}
    ]
)

print(f"Claude: {response.content[0].text}")
print(f"输入 tokens: {response.usage.input_tokens}, 输出 tokens: {response.usage.output_tokens}")
print()

# 2. 使用系统提示词
print("2. 系统提示词示例")
print("-" * 40)

response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=100,
    system="You are a helpful coding assistant. Always provide code examples.",
    messages=[
        {"role": "user", "content": "How to read a file in Python?"}
    ]
)

print(f"Claude: {response.content[0].text}")
print()

# 3. 流式响应
print("3. 流式响应示例")
print("-" * 40)
print("Claude: ", end="", flush=True)

stream = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=50,
    messages=[
        {"role": "user", "content": "Count from 1 to 5"}
    ],
    stream=True,
)

for chunk in stream:
    if chunk.type == "content_block_delta":
        print(chunk.delta.text, end="", flush=True)
print("\n")

print("✅ 完成!")
print(f"配置信息：")
print(f"  Base URL: {base_url}")
print(f"  API Key: {api_key[:20]}...")
print(f"  支持的模型: claude-3-5-sonnet-20241022, claude-3-5-haiku-20241022")